﻿using System;
using System.IO;
using System.Security.Permissions;

namespace TextFileWatcher
{
    public class Program
    {
        private const string FileName = @"file.txt";

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        private static void OnFileChanged(object sender, FileSystemEventArgs args)
        {
            Console.WriteLine("A file {0} was {1}.", args.Name, args.ChangeType);
            using (StreamReader sr = new StreamReader(args.FullPath))
            {
                Console.WriteLine(sr.ReadToEnd());
                Console.WriteLine();
            }
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Main()
        {
            if (!File.Exists(FileName))
            {
                File.Create(FileName);
            }
            using (FileSystemWatcher watcher = new FileSystemWatcher(".", FileName))
            {
                watcher.Changed += new FileSystemEventHandler(OnFileChanged);
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                    NotifyFilters.FileName | NotifyFilters.DirectoryName;
                watcher.EnableRaisingEvents = true;
                Console.ReadLine();
            }
        }
    }
}
